﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace PatientInformationExtract.Components
{
    /// <summary>
    /// The ViewModelCommand class - an ICommand that can fire a function.
    /// </summary>
    public class Command : ICommand
    {
        public bool CanExecute(object parameter)
        {
            bool canExecute = false;

            if (this.CanExecuteFunc == null)
            {
                canExecute = true;
            }
            else
            {
                canExecute = this.CanExecuteFunc(parameter);
            }
            this.CanExecuteState = canExecute;
            return canExecute;
        }

        bool _canExecuteState = true;
        public bool CanExecuteState
        {
            get
            {
                return this._canExecuteState;
            }
            set
            {
                if (!this._canExecuteState.Equals(value))
                {
                    this._canExecuteState = value;
                    if (this.CanExecuteChanged != null)
                        this.CanExecuteChanged(this, EventArgs.Empty);
                }
            }
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            if (this.ExecuteAction == null)
            {
                throw new Exception("The execute method is absent.");
            }
            else
            {
                this.ExecuteAction(parameter);
            }
        }

        public Action<object> ExecuteAction { get; private set; }
        public Func<object, bool> CanExecuteFunc { get; private set; }

        public Command(Action<object> executeAction)
        {
            this.ExecuteAction = executeAction;
        }

        public Command(Action<object> executeAction, Func<object, bool> canExecuteAction)
        {
            this.ExecuteAction = executeAction;
            this.CanExecuteFunc = canExecuteAction;
        }
    }
}
